<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增模板' : '修改模板'" :close-on-click-modal="false" width="60%">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px" @keyup.enter="submitHandle()">
			<el-form-item label="名称" prop="templateName">
				<el-input v-model="dataForm.templateName" clearable placeholder="请输入名称"></el-input>
			</el-form-item>
			<el-form-item label="路径" prop="generatorPath">
				<el-input v-model="dataForm.generatorPath" clearable placeholder="请输入路径"></el-input>
			</el-form-item>
			<el-form-item label="类型" prop="templateType">
				<el-radio-group v-model="dataForm.templateType" :disabled="dataForm.id">
					<el-radio v-for="item in TEMPLATE_TYPES" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="描述" prop="templateDesc">
				<el-input v-model="dataForm.templateDesc" clearable placeholder="请输入描述"></el-input>
			</el-form-item>
			<el-form-item v-if="dataForm.templateType === 0" label="内容" prop="templateContent">
				<el-input v-model="dataForm.templateContent" clearable type="textarea" autosize placeholder="请输入内容"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus/es'
import { templateDetailApi, templateSubmitApi } from '@/api/template'
import { TEMPLATE_TYPES } from '@/constant/enum'
import type { FormRules } from 'element-plus'

const emit = defineEmits(['refreshDataList'])

const props = defineProps({
	templateGroupId: {
		type: Number,
		required: true // 表示父组件必须传递该值
	}
})

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: null,
	templateName: '',
	templateDesc: '',
	templateContent: '',
	generatorPath: '',
	templateType: 0,
	templateGroupId: props.templateGroupId
})

const dataRules = ref<FormRules>({
	templateName: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
	generatorPath: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
	templateType: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
	templateContent: []
})

const init = (id: Number) => {
	visible.value = true
	dataForm.id = null

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
		dataForm.templateGroupId = props.templateGroupId
	}

	// id 存在则为修改
	if (id) {
		getTemplate(id)
	}
}

// 动态更新 templateContent 是否为必填项
watch(
	() => dataForm.templateType,
	newVal => {
		const rules = dataRules.value
		if (newVal === 0) {
			//清空模板内容
			dataForm.templateContent = ''
			// 设置为必填，显示红星
			rules.templateContent = [
				{
					required: true,
					message: '内容不能为空',
					trigger: 'blur'
				}
			]
		} else {
			// 非必填
			rules.templateContent = []
		}
		// 强制更新规则引用地址，确保 el-form 检测到规则变化
		dataRules.value = { ...rules }
	}
)

const getTemplate = (id: Number) => {
	templateDetailApi(id).then(res => {
		Object.assign(dataForm, res.data)
		if (dataForm.templateType === 0) {
			// 设置为必填，显示红星
			dataRules.value.templateContent = [
				{
					required: true,
					message: '内容不能为空',
					trigger: 'blur'
				}
			]
		}
	})
}

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		templateSubmitApi({ ...dataForm }).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
